<template>
  <div></div>
</template>
<script>
export default {
  name: 'industry',
  data () {
    return {
      filter: [],
      city: [],
      cityId_z: 0
    }
  },
  beforeMount () {
    this.isFilter()
    this.isCity()
  },
  methods: {
    isFilter () {
      if (this.$store.state.industry === 1 || this.$store.state.industry === null) {
        this.postFilter()
      }
    },
    isCity () {
      if (this.$store.state.city === 1 || this.$store.state.city === null) {
        this.postCity()
      }
    },
    postFilter () {
      this.$axios.post(process.env.API_HOST + '/api/common/one_industry')
        .then(this.postFilterSucc)
        .catch(error => console.log(error))
    },
    postFilterSucc (res) {
      if (res.data.code === 1) {
        this.filter = res.data.data
        for (var i = 0; i < this.filter.length; i++) {
          this.postFilterX(this.filter[i].region_id, i)
        }
        // this.$store.state.industry = filter
      }
    },
    postFilterX (id, i) {
      let that = this
      this.$axios.post(process.env.API_HOST + '/api/common/second_industry', {
        id: id
      }).then(function (res) {
        if (res.data.code === 1) {
          let data = res.data.data
          that.filter[i].children = data
          that.$store.state.industry = that.filter
        }
      })
        .catch(error => console.log(error))
    },
    postCity (i) {
      let shu = i
      let that = this
      if (that.cityId_z === 0) {
        that.cityId_z = 2
      }
      let id = that.cityId_z
      that.$axios.post(process.env.API_HOST + '/api/common/city', {
        'id': id
      }).then(function (res) {
        if (res.data.code === 1) {
          if (id === 2) {
            that.city = res.data.data
            for (var i = 0; i < that.city.length; i++) {
              that.cityId_z = that.city[i].id
              that.postCity(i)
            }
          } else {
            that.city[shu].children = res.data.data
            that.$store.state.city = that.city
          }
        }
      }).catch(error => console.log(error))
    }
  }
}
</script>
